﻿app.controller('LessonManagerController', function ($scope, $rootScope, lessonFactory, $compile, notificationFactory) {
    init();

    function init() {
        
        $scope.severFile = mainFileAddress;
        $scope.roleByGroup = JSON.parse(localStorage.getItem("menu"));
        $scope.userName = $rootScope.userName;
        //count--------------
        notificationFactory.countAddFriend(localStorage.getItem("idkey")).success(function (data) {
            $scope.countAddFriend = data;
        });
        notificationFactory.countFromRoom(localStorage.getItem("idkey")).success(function (data) {
            $scope.countFromRoom = data;
        });
        notificationFactory.countNewMessage(localStorage.getItem("idkey")).success(function (data) {
            $scope.countNewMessage = data;
        });
        
        var dataPost = {
            PageIndex: 1,
            PageSize: 6
        };
        $scope.data = {};
        $scope.lessons = {};
        $scope.hasPreviousPage = {};
        $scope.hasNextPage = {};
        $scope.totalPages = {};
        var newp;
        var dom;
        lessonFactory.getLessons(dataPost).success(function (data) {
            $scope.lessons = data.List;
            $scope.hasNextPage = data.HasNextPage;
            $scope.hasPreviousPage = data.HasPreviousPage;
            $scope.totalPages = data.TotalPages;
            dom = $('#paging');
            dom.append('<span class="current-page">' + dataPost.PageIndex + '</span>');
            if ($scope.hasNextPage) {
                newp = dataPost.PageIndex + 1;
                dom.append('<span class="next-page" ng-click=paging(' + newp + ')>»</span>');
            }
            if (newp != $scope.totalPages)
                dom.append('<span class="last-page" ng-click=paging(' + $scope.totalPages + ')>Trang cuối</span>');
            var compiled = $compile(dom);
            compiled($scope);
        });


        $scope.paging = function (newp) {
            dataPost.PageIndex = newp;
            dom.empty();
            lessonFactory.getLessons(dataPost).success(function (data) {
                $scope.lessons = data.List;
                $scope.hasNextPage = data.HasNextPage;
                $scope.hasPreviousPage = data.HasPreviousPage;
                var dom = $('#paging');
                if (newp != 1)
                    dom.append('<span class="first-page" ng-click=paging(1)>Trang đầu</span>');
                if ($scope.hasPreviousPage) {
                    dom.append('<span class="prev-page" ng-click=paging(' + (dataPost.PageIndex-1) + ')>«</span>');

                }
                dom.append('<span class="current-page">' + dataPost.PageIndex + '</span>');
                if ($scope.hasNextPage) {
                    dom.append('<span class="next-page" ng-click=paging(' + (dataPost.PageIndex + 1) + ')>»</span>');
                }
                if (newp != $scope.totalPages)
                    dom.append('<span class="last-page" ng-click=paging(' + $scope.totalPages + ')>Trang cuối</span>');
                
                var compiled = $compile(dom);
                compiled($scope);
            });

        };


        $scope.ready = true;
        createMenu();
    }

});